package com.rover.system.controller;

import com.grid.DataGrid;
import com.grid.Page;

import com.rover.system.entity.FormTableField;
import com.rover.system.entity.FormTableInfo;
import com.rover.system.service.FormTableFieldService;
import com.rover.system.service.FormTableInfoService;
import com.utils.HqlGenerateUtil;
import com.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 公开访问
 */
@Controller
@RequestMapping("/formTableFieldController")
public class FormTableFieldController {

    @Autowired
    private FormTableFieldService service;
    @Autowired
    private FormTableInfoService formTableInfoService;

    @RequestMapping("/query")
    @ResponseBody
    public Map<String, Object> query(HttpServletRequest request, Page page, HttpServletResponse response) throws Exception {
        HqlGenerateUtil.installHql(request, FormTableField.class, page);
        Map<String, Object> map = new HashMap<String, Object>();
        DataGrid dataGrid = service.query(page);
        map = JsonUtil.JSONString(dataGrid.getResult(),
                dataGrid.getTotalCount());
        return map;
    }


    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> save(FormTableField formTableField, HttpServletRequest request)
            throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        Boolean success = true;
        String message = "";
        try {
            if (service.isFieldNameRepeat(formTableField, Integer.valueOf(request.getParameter("tableId")))) {
                message = "此字段已存在，请重新输入！";
                success = false;
            } else {
                FormTableInfo formTableInfo = formTableInfoService.<FormTableInfo>getEntity(FormTableInfo.class, Integer.valueOf(request.getParameter("tableId")));
                formTableField.setFormTableInfo(formTableInfo);
                service.saveOrUpdate(formTableField);
            }
            map.put("success", success);
            map.put("message", message);
        } catch (Exception e) {
            // logger.error(e.getMessage(), e);
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public
    @ResponseBody
    Map<String, Object> delete(@RequestParam("fieldId") List<Integer> fieldId, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            //由于有连带关系，所以删除的时候要先把主表里的数据删除
            for (Integer l : fieldId) {
                FormTableField temp = service.getEntity(FormTableField.class, l);
                temp.getFormTableInfo().getFormTableField().remove(temp);
                temp.setFormTableInfo(null);
                service.delete(temp);
            }
            map.put("success", true);
            map.put("message", "");

        } catch (Exception e) {
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }


}
